package com.zhenta.controller;

import com.zhenta.service.AppointmentService;
import com.zhentao.pojo.Appointment;
import com.zhentao.pojo.AppointmentVO;
import com.zhentao.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约挂号控制器
 */
@RestController
@RequestMapping("/appointment")
@CrossOrigin(origins = "*")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    /**
     * 获取预约挂号列表
     */
    @GetMapping("/list")
    public Result<List<AppointmentVO>> getAppointmentList() {
        try {
            List<AppointmentVO> appointmentList = appointmentService.getAppointmentList();
            return Result.success(appointmentList);
        } catch (Exception e) {
            return Result.error("获取预约挂号列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取预约挂号详情
     */
    @GetMapping("/{id}")
    public Result<AppointmentVO> getAppointmentById(@PathVariable Integer id) {
        try {
            AppointmentVO appointment = appointmentService.getAppointmentById(id);
            if (appointment != null) {
                return Result.success(appointment);
            } else {
                return Result.error("预约挂号信息不存在");
            }
        } catch (Exception e) {
            return Result.error("获取预约挂号详情失败：" + e.getMessage());
        }
    }

    /**
     * 根据就诊人ID获取预约挂号列表
     */
    @GetMapping("/patient/{patientId}")
    public Result<List<AppointmentVO>> getAppointmentsByPatientId(@PathVariable Integer patientId) {
        try {
            List<AppointmentVO> appointmentList = appointmentService.getAppointmentsByPatientId(patientId);
            return Result.success(appointmentList);
        } catch (Exception e) {
            return Result.error("获取就诊人预约挂号列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据状态获取预约挂号列表
     */
    @GetMapping("/status/{status}")
    public Result<List<AppointmentVO>> getAppointmentsByStatus(@PathVariable Integer status) {
        try {
            List<AppointmentVO> appointmentList = appointmentService.getAppointmentsByStatus(status);
            return Result.success(appointmentList);
        } catch (Exception e) {
            return Result.error("获取预约挂号列表失败：" + e.getMessage());
        }
    }

    /**
     * 创建预约挂号
     */
    @PostMapping("/create")
    public Result<String> createAppointment(@RequestBody Appointment appointment) {
        try {
            // 数据验证
            if (appointment.getUid() == null) {
                return Result.error("就诊人ID不能为空");
            }
            if (appointment.getHospitalname() == null || appointment.getHospitalname().trim().isEmpty()) {
                return Result.error("医院名称不能为空");
            }
            if (appointment.getDeptId() == null) {
                return Result.error("科室ID不能为空");
            }
            if (appointment.getDoctorId() == null) {
                return Result.error("医生ID不能为空");
            }
            if (appointment.getByData() == null) {
                return Result.error("就诊日期不能为空");
            }
            if (appointment.getCost() == null) {
                return Result.error("费用不能为空");
            }

            boolean success = appointmentService.createAppointment(appointment);
            if (success) {
                return Result.success("预约挂号成功");
            } else {
                return Result.error("预约挂号失败");
            }
        } catch (Exception e) {
            return Result.error("预约挂号失败：" + e.getMessage());
        }
    }

    /**
     * 取消预约挂号
     */
    @PutMapping("/cancel/{id}")
    public Result<String> cancelAppointment(@PathVariable Integer id) {
        try {
            boolean success = appointmentService.cancelAppointment(id);
            if (success) {
                return Result.success("取消预约成功");
            } else {
                return Result.error("取消预约失败");
            }
        } catch (Exception e) {
            return Result.error("取消预约失败：" + e.getMessage());
        }
    }

    /**
     * 完成预约挂号
     */
    @PutMapping("/complete/{id}")
    public Result<String> completeAppointment(@PathVariable Integer id) {
        try {
            boolean success = appointmentService.completeAppointment(id);
            if (success) {
                return Result.success("完成预约成功");
            } else {
                return Result.error("完成预约失败");
            }
        } catch (Exception e) {
            return Result.error("完成预约失败：" + e.getMessage());
        }
    }

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public Result<String> health() {
        return Result.success("预约挂号服务运行正常");
    }
} 